<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>声明周期</title>
    <style>
        .img-container{
            width: 800px;
        }
        img{
            width: 100%;
        }
    </style>
</head>
<body>
    <button>来个看图神器</button>
    <button>下去吧</button>
    <hr>

    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class Image extends React.Component {
            state = {
                src: 'http://api.xiaohigh.com:8090/image/random'
            }
            render(){
                return <div className="img-container" onClick={this.change}>
                        <img src={this.state.src} alt="" />
                    </div>
            }
            change = () => {
                this.setState({
                    src: 'http://api.xiaohigh.com:8090/image/random?t='+Date.now() //添加一个额外的参数, 使得 url 与之前的不一样, 避免缓存
                })
            }

            //组件挂载完毕 生命周期回调
            componentDidMount(){
                console.log('组件挂载完毕啦!!'); //初始化工作
            }

            //组件将要卸载
            componentWillUnmount(){
                console.log('组件将要卸载...');  //收尾工作
            }
        }

        //获取按钮
        let btns = document.querySelectorAll('button');

        btns[0].onclick = function(){
            //显示图片的组件
            ReactDOM.render(<Image />, document.querySelector('#root'));
        }

        btns[1].onclick = function(){
            //卸载组件 unmount 卸载  component 组件  at 在  node 节点
            ReactDOM.unmountComponentAtNode(document.querySelector('#root'));
        }


    </script>
</body>
</html>